package io.persimmon.core.module.calculation.instance;

import io.persimmon.core.module.calculation.Calculation;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

@NoArgsConstructor
public class CountCalculation implements Calculation<Integer, Number> {

    private AtomicInteger count = new AtomicInteger(0);

    @Getter
    private Consumer<Integer> resultConsumer;

    public CountCalculation(Consumer<Integer> resultConsumer) {
        this.resultConsumer = resultConsumer;
    }

    @Override
    public void accept(Number data) {
        count.getAndAdd(1);
    }

    @Override
    public Integer calculationResult() {
        return count.get();
    }

    @Override
    public void clean() {
        count.set(0);
    }
}
